﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;
using Portal.Workflow.Library;

namespace Portal.Systems.Bussiness
{
    public class WorkflowDefProcessBll 
    {
        public static WorkflowDefCls[] ReadingWorkflowDef(string SiteId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableWorkflowDef order by WorkFlowName", null);
            if (dsResult.Tables[0].Rows.Count ==  0)
            {
                return new WorkflowDefCls[0];
            }
            WorkflowDefCls[] CmsWorkflowDefs = WorkflowDefParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsWorkflowDefs;
        }

        public static WorkflowDefCls CreateWorkflowDefModel(string SiteId, string WorkflowDefId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableWorkflowDef " +
                                               "     where WorkflowDefId=@WorkflowDefId",
                    new DbParam[] 
                    { 
                        new DbParam("WorkflowDefId", WorkflowDefId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            WorkflowDefCls OWorkflowDef = WorkflowDefParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OWorkflowDef;
        }


        public static WorkflowDefMapRoleCls[] ReadingWorkflowDefMapRoles(
            string SiteId, 
            string WorkflowDefId,
            RoleCls[] WorkflowRoles)
        {
            WorkflowDefMapRoleCls[] WorkflowDefMapRoles = new WorkflowDefMapRoleCls[0];
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            IDbTransaction Trans = DBService.BeginTransaction();
            try
            {
                string DelNotInWorkflow = "(";
                for (int iIndex = 0; iIndex < WorkflowRoles.Length; iIndex++)
                {
                    DelNotInWorkflow = DelNotInWorkflow + "'" + WorkflowRoles[iIndex].RoleId + "'";
                    if (iIndex < WorkflowRoles.Length - 1)
                    {
                        DelNotInWorkflow = DelNotInWorkflow + ",";
                    }
                }
                DelNotInWorkflow = DelNotInWorkflow + ")";
                if (!DelNotInWorkflow.Equals("()"))
                {
                    string DelQuery = " Delete from TableWorkflowDefMapRole where frkWorkflowDefId=@WorkflowDefId and not WorkflowRoleId in " + DelNotInWorkflow;
                    DBService.ExecuteNonQuery(Trans, DelQuery, new DbParam[] { new DbParam("WorkflowDefId", WorkflowDefId) });
                }

                //doan nay insert vao
                for (int iIndex = 0; iIndex < WorkflowRoles.Length; iIndex++)
                {
                    try
                    {
                        string WorkflowDefMapRoleId = System.Guid.NewGuid().ToString();
                        DBService.Insert(Trans, "TableWorkflowDefMapRole", new DbParam[]{
                            new DbParam("WorkflowDefMapRoleId",WorkflowDefMapRoleId),
                            new DbParam("frkWorkflowDefId",WorkflowDefId),
                            new DbParam("WorkflowRoleId",WorkflowRoles[iIndex].RoleId),
                            new DbParam("MapGroupRoleId",null),
                            new DbParam("WorkflowRoleName",WorkflowRoles[iIndex].RoleName),
                        });
                    }
                    catch (Exception ex)
                    {
                        ex.Source = "";
                    }
                    DBService.ExecuteNonQuery(Trans, " Update TableWorkflowDefMapRole set WorkflowRoleName=@WorkflowRoleName where MapGroupRoleId=@MapGroupRoleId", new DbParam[]{
                        new DbParam("MapGroupRoleId",WorkflowRoles[iIndex].RoleId),
                        new DbParam("WorkflowRoleName",WorkflowRoles[iIndex].RoleName),
                    });
                }
                
                DataSet dsResult =
                   DBService.GetDataSet(Trans, "select * from TableWorkflowDefMapRole " +
                                              "     where frkWorkflowDefId=@WorkflowDefId",
                   new DbParam[] 
                    { 
                        new DbParam("WorkflowDefId", WorkflowDefId),
                    });
                WorkflowDefMapRoles = WorkflowDefMapRoleParser.ParseFromDataTable(dsResult.Tables[0]);
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
            return WorkflowDefMapRoles;
        }


        public static WorkflowDefMapRoleCls[] ReadingWorkflowDefMapRoles(
            string SiteId,
            string WorkflowDefId)
        {
            WorkflowDefMapRoleCls[] WorkflowDefMapRoles = new WorkflowDefMapRoleCls[0];
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            IDbTransaction Trans = DBService.BeginTransaction();
            try
            {
                DataSet dsResult =
                   DBService.GetDataSet(Trans, "select * from TableWorkflowDefMapRole " +
                                              "     where frkWorkflowDefId=@WorkflowDefId",
                   new DbParam[] 
                    { 
                        new DbParam("WorkflowDefId", WorkflowDefId),
                    });
                WorkflowDefMapRoles = WorkflowDefMapRoleParser.ParseFromDataTable(dsResult.Tables[0]);
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
            return WorkflowDefMapRoles;
        }


        public static void SaveWorkflowDefMapRoles(
            string SiteId, 
            string WorkflowDefId,
            WorkflowDefMapRoleCls[] WorkflowDefMapRoles)
        {
            WorkflowDefCls OWorkflowDef = CreateWorkflowDefModel(SiteId, WorkflowDefId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            IDbTransaction Trans = DBService.BeginTransaction();
            try
            {
                //doan nay insert vao
                for (int iIndex = 0; iIndex < WorkflowDefMapRoles.Length; iIndex++)
                {
                    DBService.Update(Trans, "TableWorkflowDefMapRole", "WorkflowDefMapRoleId", WorkflowDefMapRoles[iIndex].WorkflowDefMapRoleId, new DbParam[]{
                        new DbParam("MapGroupRoleId",WorkflowDefMapRoles[iIndex].MapGroupRoleId),
                    });
                }
                
                //WorkFlowApp wfa = new WorkFlowApp(OWorkflowDef.XmlData);
                //for (int iIndexRole = 0; iIndexRole < WorkflowDefMapRoles.Length; iIndexRole++)
                //{
                //    wfa.ChangeRole(WorkflowDefMapRoles[iIndexRole].WorkflowRoleId, WorkflowDefMapRoles[iIndexRole].MapGroupRoleId);
                //}
                //string XmlData = wfa.GetXml();
                //DBService.Update(Trans, "TableWorkflowDef", "WorkflowDefId", WorkflowDefId,
                //    new DbParam[]{
                //        new DbParam("XmlData",XmlData),
                //    });
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
        }


        public static WorkflowControlCls[] ReadingWorkflowControls(string SiteId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select TableWorkflowControl.*,DataLanguageCode=DataLanguage from TableWorkflowControl left outer join TableDataLanguage on TableDataLanguage.DataLanguageId=frkDataLanguageId order by SortIndex", null);
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new WorkflowControlCls[0];
            }
            WorkflowControlCls[] WorkflowControls = WorkflowControlParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return WorkflowControls;
        }

        public static void SaveWorkflowControls(string SiteId, WorkflowControlCls[] WorkflowControls)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            IDbTransaction Trans = DBService.BeginTransaction();
            try
            {

                for (int iIndex = 0; iIndex < WorkflowControls.Length; iIndex++)
                {
                    DBService.Update(Trans, "TableWorkflowControl", "WorkflowControlId", WorkflowControls[iIndex].WorkflowControlId, new DbParam[]{
                        new DbParam("WorkflowDefId",WorkflowControls[iIndex].WorkflowDefId),
                    });
                }
                Trans.Commit();
            }
            catch (Exception ex)
            {
                Trans.Rollback();
                throw (ex);
            }
        }

        public static WorkflowDefCls CreateWorkflowDefFromWorkflowControl(string SiteId,string WorkflowControlId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            
            DataSet dsResult =
                    DBService.GetDataSet(null, 
                    " select TableWorkflowDef.* from TableWorkflowControl "+
                    "   join TableWorkflowDef on TableWorkflowDef.WorkflowDefId = TableWorkflowControl.WorkflowDefId"+
                    " where WorkflowControlId=@WorkflowControlId",new DbParam[]{
                        new DbParam("WorkflowControlId",WorkflowControlId)
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            WorkflowDefCls OWorkflowDef = WorkflowDefParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            return OWorkflowDef;
        }


        public static WorkflowControlCls CreateWorkflowControlModel(string SiteId,string WorkflowControlId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select TableWorkflowControl.*,DataLanguageCode=DataLanguage from TableWorkflowControl left outer join TableDataLanguage on TableDataLanguage.DataLanguageId=frkDataLanguageId where WorkflowControlId=@WorkflowControlId", new DbParam[] { new DbParam("WorkflowControlId", WorkflowControlId) });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            WorkflowControlCls OWorkflowControl = WorkflowControlParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OWorkflowControl;
        }
    }
}

